using UnityEngine;

public class NpcMove : NpcAction
{
    private bool _walkState;
    protected override void OnEnter()
    {
        base.OnEnter();
        _npc.PlayAnimation("Walk");
        this._npc.SetAnimationSpeed(1f);
        _walkState = true;
    }
    protected override void OnUpdate(float deltaTime, float progress)
    {
        base.OnUpdate(deltaTime, progress);
        var len = _npc.MoveDirection.magnitude;
        var walk = true;
        if (len > 0.5f)
        {
            walk = false;
            this._npc.SetAnimationSpeed(Mathf.Lerp(0.2f, 1f, len));
        }
        else
        {
            walk = true;
            this._npc.SetAnimationSpeed(Mathf.Lerp(0.2f, 1.5f, len));
        }
        if (_walkState != walk)
        {
            _walkState = walk;
            if (_walkState)
            {
                _npc.PlayAnimation("Walk");
            }
            else
            {
                _npc.PlayAnimation("Run");
            }
        }
        var dir = _npc.MoveDirection;
        var display = _npc.GetDisplay();
        var agent = _npc.GetNavmeshAgent();
        var forward = Quaternion.LookRotation(dir.normalized);
        display.transform.rotation = Quaternion.Slerp(display.transform.rotation, forward, agent.angularSpeed / 120f * deltaTime);
        var dotValue = Mathf.Max(0f, Vector3.Dot(dir.normalized, display.transform.forward));
        dir *= dotValue * dotValue;
        var moveDelta = dir * agent.speed * deltaTime;
        agent.Warp(display.transform.position + moveDelta);
    }
    protected override void OnExit()
    {
        base.OnExit();
        this._npc.SetAnimationSpeed(1f);
    }
}